/** message box **/
var users = {
        "pyttop": 93198,
        "huypham": 96813
    },

    $messageWrap = $("<div id='cheater-msg'></div>").css({
    "position": "fixed",
    "left": 0,
    "top": 0,
    "padding": "10px 20px 10px",
    "font-size": "18px",
    "color": "blue",
    "background": "#eee",
    "box-shadow": "0 0 12px -4px #333"
}).appendTo($(document.body)),
    $msg = $("<div class='content' style='padding: 5px 0'>loading ...</div>").appendTo($messageWrap);

$("<div id='copyright' style='padding: 5px 0 0; text-align: center'>Cheating by Pyttop</div>").css({
    "font-size": "12px",
    "color": "#C6BB63",
    "border-top": "1px solid #fff"
}).appendTo($messageWrap);

$(function () {
    var pub = {};
    pub.waiting = function (condition, execution) {
        var waiter = setInterval(function () {
            if (condition()) {
                clearInterval(waiter);
                execution();
            }
        }, 1000);
    };

    function showMsg($content, $error) {
        $msg.html($content);
        if ($error) {
            $msg.css("color", "red");
        } else {
            $msg.css("color", "blue");
        }
    }

    function currentUser() {
        var $userWrap = $("#user");

        var m = /<p><font color=['"]darkgreen["']>Hi ([^.]+).<\/font>/i.exec($userWrap.html());
        if (m) {
            return m[1];
        } else {
            console.log("User not found. Debug info: ");
            console.log($userWrap.html());
            return null;
        }
    }

    pub.msg = function ($content) {
        showMsg($content, false);
    };

    pub.err = function($content) {
        showMsg($content, true);
    };


    pub.complete = function () {
        pub.msg("Cheating completed!");
        return null;
    };

    pub.reload = function () {
        $msg.text($msg.text() + " --- Reloading ...");
        setTimeout("window.location.reload()", 1000);
        return null;
    };


    // nếu đã đăng nhập, thực hiện chờ đợi hàm window.main_function xuất hiện
    var c = currentUser();
    if (c && users[c] != undefined) {
        pub.userId = users[c];
        pub.waiting(function () {
            return window["cheater"] != undefined;
        }, function () {
            // Goi thuc thi ham Cheat
            window["cheater"](pub);
        });
    } else {
        pub.err("Not login or User is not found!");
    }

});